"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ignoreFile = exports.createIgnoreFilter = exports.shouldIgnoreFile = void 0;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const ignore_1 = __importDefault(require("ignore"));
const findup_sync_1 = __importDefault(require("findup-sync"));
const ignoreList = [
    'fields.output.json',
    'hubspot.config.yml',
    'hubspot.config.yaml',
    'node_modules',
    '.*',
    '*.log',
    '*.swp',
    '.env',
    // # macOS
    'Icon\\r',
    '__MACOSX',
    // # Linux
    '~',
    // # Emacs
    '*~',
    // # Windows
    'Thumbs.db',
    'ehthumbs.db',
    'Desktop.ini',
    '@eaDir', // Synology Diskstation "hidden" folder where the server stores thumbnails
];
const ignoreRules = (0, ignore_1.default)().add(ignoreList);
let searchDomain = null;
let loaded = false;
function loadIgnoreConfig(isInProject = false) {
    if (loaded) {
        return;
    }
    // Temporary solution to improve serverless beta: https://git.hubteam.com/HubSpot/cms-devex-super-repo/issues/2
    // Do not do this when in a developer project b/c we want the package-lock.json file uploaded.
    if (!isInProject) {
        ignoreRules.add('package-lock.json');
    }
    const file = (0, findup_sync_1.default)('.hsignore');
    if (file) {
        if (fs_1.default.existsSync(file)) {
            ignoreRules.add(fs_1.default.readFileSync(file).toString());
            searchDomain = path_1.default.dirname(file);
        }
    }
    loaded = true;
}
function shouldIgnoreFile(file, isInProject = false) {
    loadIgnoreConfig(isInProject);
    const relativeTo = searchDomain || '/';
    const relativePath = path_1.default.relative(relativeTo, file);
    return !!relativePath && ignoreRules.ignores(relativePath);
}
exports.shouldIgnoreFile = shouldIgnoreFile;
function createIgnoreFilter(isInProject) {
    loadIgnoreConfig(isInProject);
    return (file) => !shouldIgnoreFile(file);
}
exports.createIgnoreFilter = createIgnoreFilter;
function ignoreFile(filePath) {
    ignoreRules.add(filePath);
}
exports.ignoreFile = ignoreFile;
